<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="分类" prop="categoryIds">
        <el-cascader style="width: 100%" v-model="queryParams.categoryIds" :options="categoryOptions" :props="props" collapse-tags clearable
          ><template slot-scope="{ node, data }">
            <span>{{ data.label }}</span>
            <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
          </template></el-cascader
        >
      </el-form-item>
      <el-form-item label="信件编号" prop="letterNumber">
        <el-input v-model="queryParams.letterNumber" placeholder="请输入信件编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="属地" prop="deptId" class="deptIdClass">
        <div>
          <treeselect
            v-model="queryParams.deptId"
            :options="deptOptions"
            :show-count="true"
            placeholder="请选择属地"
          />
        </div>
      </el-form-item> -->
      <el-form-item label="属地" prop="deptId">
        <el-select v-model="queryParams.deptId" placeholder="请选择属地" clearable>
          <el-option v-for="dict in dict.type.biz_area_owner" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="登记机构" prop="signDept">
        <el-input v-model="queryParams.signDept" placeholder="请输入登记机构" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="姓名" prop="letterUsername">
        <el-input v-model="queryParams.letterUsername" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="letterPhone">
        <el-input v-model="queryParams.letterPhone" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否初访" prop="isVisit">
        <el-select v-model="queryParams.isVisit" placeholder="是否初访" clearable>
          <el-option v-for="dict in dict.type.biz_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否化解" prop="isSolve">
        <el-select v-model="queryParams.isSolve" placeholder="是否化解" clearable>
          <el-option v-for="dict in dict.type.biz_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否办结" prop="isFinish">
        <el-select v-model="queryParams.isFinish" placeholder="是否办结" clearable>
          <el-option v-for="dict in dict.type.biz_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否有理" prop="isReasonable">
        <el-select v-model="queryParams.isReasonable" placeholder="是否有理" clearable>
          <el-option v-for="dict in dict.type.biz_reasonable" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否涉法涉诉" prop="isLaw">
        <el-select v-model="queryParams.isLaw" placeholder="是否涉法涉诉" clearable>
          <el-option v-for="dict in dict.type.biz_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="信访目的" prop="letterPurpose">
        <el-select v-model="queryParams.letterPurpose" placeholder="信访目的" clearable>
          <el-option v-for="dict in dict.type.biz_letter_purpose" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="认领状态" v-if="display === 'register'" prop="status">
        <el-select v-model="queryParams.status" placeholder="认领状态" clearable>
          <el-option v-for="dict in dict.type.biz_letter_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="分类状态" prop="isCategory">
        <el-select v-model="queryParams.isCategory" placeholder="分类状态" clearable style="width: 100%">
          <el-option v-for="dict in dict.type.biz_whether_category" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="台账月份" prop="billMonth">
        <el-date-picker v-model="queryParams.billMonth" type="month" placeholder="选择月" value-format="yyyy-MM"> </el-date-picker>
      </el-form-item>
      <el-form-item label="登记时间">
        <el-date-picker v-model="letterTime" :clearable="true" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" @input="$forceUpdate()"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-refresh" size="mini" :disabled="multiple" @click="handleReset" v-hasPermi="['city:letter:reset']">重置信访件</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="letterList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="一级分类" align="center" prop="firstCategoryName" />
      <el-table-column label="二级分类" align="center" prop="secondCategoryName" />
      <el-table-column label="台账月份" align="center" prop="billMonth" />
      <el-table-column label="信件编号" align="center" prop="letterNumber" width="100" />
      <el-table-column label="类别" align="center" prop="caseType" />
      <el-table-column label="信件标题" align="center" prop="letterTitle" :show-overflow-tooltip="true" />
      <!-- <el-table-column
        label="属地"
        align="center"
        prop="deptName"
        width="130"
      /> -->

      <el-table-column label="属地" align="center" prop="deptId" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_area_owner" :value="scope.row.deptId" />
        </template>
      </el-table-column>
      <el-table-column label="问题属地" align="center" prop="ownerArea" width="150" />
      <el-table-column label="登记机构" align="center" prop="signDept" width="150" />
      <el-table-column label="登记时间" align="center" prop="letterTime" width="150" />
      <!-- <el-table-column
        label="问题属地"
        align="center"
        prop="ownerArea"
        width="150"
      /> -->
      <el-table-column label="涉及机构" align="center" prop="referDept" width="150" :show-overflow-tooltip="true" />
      <el-table-column label="姓名" align="center" prop="letterUsername" />
      <el-table-column label="手机号" align="center" prop="letterPhone" />
      <el-table-column label="是否初访" align="center" prop="isVisit">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_yes_no" :value="scope.row.isVisit" />
        </template>
      </el-table-column>
      <el-table-column label="重访次数" align="center" prop="repeatVisit" />
      <el-table-column label="信访目的" align="center" prop="letterPurpose" width="130">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_letter_purpose" :value="scope.row.letterPurpose" />
        </template>
      </el-table-column>
      <el-table-column label="内容分类" align="center" prop="letterCategory" :show-overflow-tooltip="true" />
      <el-table-column label="投诉内容" align="center" prop="letterContent" :show-overflow-tooltip="true" />

      <el-table-column label="认领状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_letter_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <!-- 
      <el-table-column
        label="概况信息"
        align="center"
        prop="letterInfo"
        :show-overflow-tooltip="true"
      /> -->
      <el-table-column label="是否有理" align="center" prop="isReasonable">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_reasonable" :value="scope.row.isReasonable" />
        </template>
      </el-table-column>
      <el-table-column label="是否涉法涉诉" align="center" prop="isLaw" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_yes_no" :value="scope.row.isLaw" />
        </template>
      </el-table-column>
      <el-table-column label="是否化解" align="center" prop="isSolve">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_yes_no" :value="scope.row.isSolve" />
        </template>
      </el-table-column>
      <el-table-column label="是否办结" align="center" prop="isFinish">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_yes_no" :value="scope.row.isFinish" />
        </template>
      </el-table-column>
      <el-table-column label="是否重复认领" align="center" prop="isRepeatClaim" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_yes_no" :value="scope.row.isRepeatClaim" />
        </template>
      </el-table-column>
      <el-table-column label="认领人" align="center" prop="claimBy" />
      <el-table-column label="认领时间" align="center" prop="claimTime" width="150" />
      <el-table-column label="调度人" align="center" prop="dispatchBy" />
      <el-table-column label="调度时间" align="center" prop="dispatchTime" width="150" />
      <el-table-column label="导入时间" align="center" prop="createTime" width="150" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleUpdate(scope.row)" v-hasPermi="['letter:claim:list']">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改地市信访对话框 -->
    <el-dialog title="重复认领列表" :visible.sync="open" width="50%" append-to-body>
      <el-table v-loading="loading" :data="claimList">
        <el-table-column label="信件编号" align="center" prop="letterNumber" />
        <el-table-column label="一级分类" align="center" prop="firstCategoryName" />
        <el-table-column label="二级分类" align="center" prop="secondCategoryName" />
        <el-table-column label="是否有理" align="center" prop="isReasonable">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.biz_reasonable" :value="scope.row.isReasonable" />
          </template>
        </el-table-column>
        <!-- <el-table-column label="是否化解" align="center" prop="isSolve">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.biz_yes_no"
              :value="scope.row.isSolve"
            />
          </template>
        </el-table-column>
        <el-table-column label="是否办结" align="center" prop="isFinish">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.biz_yes_no"
              :value="scope.row.isFinish"
            />
          </template>
        </el-table-column> -->

        <el-table-column label="信访备注" align="center" prop="letterRemark" />
        <el-table-column label="认领人" align="center" prop="claimBy" />
        <el-table-column label="认领时间" align="center" prop="claimTime"> </el-table-column>
        <!-- <el-table-column label="调度时间" align="center" prop="dispatchTime">
        </el-table-column> -->
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { listLetter, resetLetter } from "@/api/hades/city/letter";
import { listAllClaim } from "@/api/hades/letter/claim";
import { deptTreeSelect } from "@/api/system/dept";
import { treeselect } from "@/api/hades/category/category";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Letter",
  dicts: ["biz_yes_no", "biz_reasonable", "biz_letter_status", "biz_letter_purpose", "biz_area_owner"],
  components: { Treeselect },
  data() {
    return {
      props: { multiple: true },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 地市信访表格数据
      letterList: [],
      // 信访认领表格数据
      claimList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pCategoryId: null,
        sCategoryId: null,
        letterNumber: null,
        signDept: null,
        caseType: null,
        deptName: null,
        letterTime: null,
        letterUsername: null,
        letterPhone: null,
        isVisit: null,
        letterPurpose: null,
        billMonth: null,
        categoryIds: [],
        exportType: null,
        statusList: [],
        deptId: null,
        isRepeatClaim: null,
        letterId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      letterTime: "",
      categoryOptions: [],
      display: "",
      // 部门树选项
      deptOptions: undefined,
      view: 2,
    };
  },
  created() {
    this.display = this.$route.query.router;
    this.getList();
    this.getCategoryList();
    this.getDeptTree();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    /** 查询分类列表 */
    getCategoryList() {
      treeselect().then((response) => {
        this.categoryOptions = response.data;
      });
    },
    /** 查询地市信访列表 */
    getList() {
      this.loading = true;
      this.queryParams.isRepeatClaim = "1";
      this.queryParams.exportType = this.display.toUpperCase();
      this.addDateRange(this.queryParams, this.letterTime, "LetterTime");
      listLetter(this.queryParams).then((response) => {
        this.letterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pCategoryId: null,
        sCategoryId: null,
        letterNumber: null,
        caseType: null,
        letterTitle: null,
        signDept: null,
        letterTime: null,
        letterUsername: null,
        letterPhone: null,
        repeatVisit: null,
        isVisit: null,
        letterPurpose: null,
        letterCategory: null,
        ownerArea: null,
        referDept: null,
        letterContent: null,
        letterInfo: null,
        letterRemark: null,
        deptId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        isReasonable: null,
        isSolve: null,
        isFinish: null,
        categoryIds: [],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.letterTime = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.queryParams.letterId = id;
      this.loading = true;
      listAllClaim(this.queryParams).then((response) => {
        this.claimList = response.data;
        this.open = true;
        this.loading = false;
      });
    },
    handleReset(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认重置地市信访件数据项？")
        .then(function () {
          return resetLetter(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        })
        .catch(() => {});
    },
  },
};
</script>
